import Eureka

//更新工作经验表单

class updateWorkExperienceController : FormViewController {

    private let waiting=waitingAlertUtil(msg: "请稍后..")

    //传过来的work
    var work:workExperience = workExperience()
    //传过来的resumeId
    var resumeId:String = String()
    
	override func viewDidLoad() {
        super.viewDidLoad()

        // 日期转换器
        let DateFormatter=NSDateFormatter()
        DateFormatter.dateFormat="yyyy-MM-dd"

        URLRow.defaultCellUpdate = { cell, row in cell.textField.textColor = .blueColor() }
        LabelRow.defaultCellUpdate = { cell, row in cell.detailTextLabel?.textColor = .orangeColor()  }
        CheckRow.defaultCellSetup = { cell, row in cell.tintColor = .orangeColor() }
        // 设置最大最小日期
        DateRow.defaultRowInitializer = { row in 
            row.maximumDate = DateFormatter.dateFromString("2015-12-31")
            row.minimumDate = DateFormatter.dateFromString("1950-01-01")
        }

        form +++ Section()
            <<< TextRow("companyName"){ row in
                row.title = "公司名称"
                row.placeholder = "输入公司名称"
            }
            <<< PushRow<String>("companyNature"){
                $0.title = "公司性质"
                $0.selectorTitle = "公司性质"
                var array=enumUtil.getOrderedStringArray(enumUtil.COMPANY_NATURE)
                $0.options = array
                $0.value = array[0]
            }
            <<< PushRow<String>("companySize"){
                $0.title = "公司规模"
                $0.selectorTitle = "公司规模"
                var array=enumUtil.getOrderedStringArray(enumUtil.COMPANY_SCALE)
                $0.options = array
                $0.value = array[0]
            }
            <<< PushRow<String>("companyIndustry"){
                $0.title = "所属行业"
                $0.selectorTitle = "所属行业"
                var array=enumUtil.getOrderedStringArray(enumUtil.INDUSRTY_ENUM)
                $0.options = array
                $0.value = array[0]
            }
            <<< TextRow("subFunction"){ row in
                row.title = "担任职位"
                row.placeholder = "输入职位"
            }
            <<< TextAreaRow("responsiblity"){
                $0.title = "工作描述"
                $0.placeholder = "输入工作描述"
            }
            
            +++ Section()
            <<< DateRow("startdate"){
                $0.title = "开始时间"
                $0.value = DateFormatter.dateFromString("2010-01-01")
            }
            <<< DateRow("enddate"){
                $0.title = "结束时间"
                $0.value = DateFormatter.dateFromString("2010-01-01")
            }
            
            +++ Section()
            <<< ButtonRow() { (row: ButtonRow) -> Void in
                   row.title = "保存"
                row.cell.tintColor = UIColor.hexStringToColor("#ffffff")
                row.cell.backgroundColor = UIColor.hexStringToColor("#ff4081")
                }  .onCellSelection({ (cell, row) in
                    self.submitForm()
                })
        +++ Section()
            <<< ButtonRow() { (row: ButtonRow) -> Void in
                   row.title = "删除"
                row.cell.tintColor = UIColor.hexStringToColor("#ff4081")
                }  .onCellSelection({ (cell, row) in
                    self.delete()
                })

        loadData()
    }

    private func loadData(){

        if(work.WorkId != ""){
            var tr:TextRow
            var tar:TextAreaRow
            var pr:PushRow<String>
            var dr:DateRow

            tr=form.rowByTag("companyName")!
            tr.value=work.CompanyName
            pr=form.rowByTag("companyNature")!
            pr.value=enumUtil.getStringByKey(Int(work.CompanyNature)!,dict: enumUtil.COMPANY_NATURE)
            pr=form.rowByTag("companySize")!
            pr.value=enumUtil.getStringByKey(Int(work.CompanySize)!,dict: enumUtil.COMPANY_SCALE)
            pr=form.rowByTag("companyIndustry")!
            pr.value=enumUtil.getStringByKey(Int(work.CompanyIndustry)!,dict: enumUtil.INDUSRTY_ENUM
            )
            tr=form.rowByTag("subFunction")!
            tr.value=work.SubFunction
            tar=form.rowByTag("responsiblity")!
            tar.value=work.Responsiblity

            let DateFormatter=NSDateFormatter()
            DateFormatter.dateFormat="yyyy-M-d H:mm:ss"
            dr=form.rowByTag("startdate")!
            dr.value=DateFormatter.dateFromString(work.StartDate)
            dr=form.rowByTag("enddate")!
            dr.value=DateFormatter.dateFromString(work.EndDate)
        }
    }


    private func submitForm(){
        // 等待提示
        waiting.show(self)
        //获取表单结果
        let dict = form.values()
        
        //由segue传过来的resumeId
//        let resumeId = self.resumeId
        let companyName = dict["companyName"] as! String
        let companyNature = enumUtil.getKeyByString(dict["companyNature"] as! String,dict: enumUtil.COMPANY_NATURE)
        let companySize = enumUtil.getKeyByString(dict["companySize"] as! String,dict: enumUtil.COMPANY_SCALE)
        let companyIndustry = String( enumUtil.getKeyByString(dict["companyIndustry"] as! String,dict: enumUtil.INDUSRTY_ENUM) )
        
        let startdate = dict["startdate"] as! NSDate
        let enddate = dict["enddate"] as! NSDate
        
        let subFunction = dict["subFunction"] as! String
        let responsiblity = dict["responsiblity"] as! String
        
        resumeManager.UpdateWorkExperience(startdate,
                             enddate:enddate,
                             companyName:companyName,
                             companyNature:companyNature,
                             companySize:companySize,
                             companyIndustry:companyIndustry,
                             subFunction:subFunction,
                             responsiblity:responsiblity,
                             workId:work.WorkId,
                             successCallback: { (msg) in
                    //            添加信息ok
                                self.waiting.stop()
                    //            退出
                                self.navigationController?.popViewControllerAnimated(true)
                                }) { (error) in
                                    self.waiting.stop()
                                    warningAlertUtil(title: "错误", msg: error).show(self)
                            }
    }


    private func delete(){
        // 等待提示
        waiting.show(self)

        resumeManager.DeleteWorkExpericeById(work.WorkId,
                             resumeId:resumeId,
                             successCallback: { (msg) in
                    //            添加信息ok
                                self.waiting.stop()
                    //            退出
                                self.navigationController?.popViewControllerAnimated(true)
                                }) { (error) in
                                    self.waiting.stop()
                                    warningAlertUtil(title: "错误", msg: error).show(self)
                            }
    }

}


